<?php

/** Data structure for subari.
 *  A view contains subari. This data structure is created when a view new
 *  view is inserted to view stack in dzViewStack.php
 *
 *  This code is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *  @author Henri Ranki & Jyry Kuukkanen
 *
 *  $Id: dzsubari.php 595 2006-04-03 16:00:47Z jyry $
 */


include_once("sodebug.php");
include_once("sogeneric.php");
include_once("dzgeneric.php");


/* Defines */
define(DZ_SUBARI_EXT, "sub");


/** Data structure for subari and methods for constructing it.
 *  @var soSet/soSet $__Items Subari items. Key is the title and value is
 *       soSet of properties
 */
class dzSubari {
    var $__Items;


    /** constructor
     *  Reads in the subari definition from given file. The html file is
     *  find out through subari file name that matches the form name. If this
     *  file does not exist, then no subari is created. Subari file name is
     *  html_file_name.sub
     *  @param string $Html Html file which subari definition is read
     *  @param int $UserLevel Users level.
     *  @return dzSubari An object that holds subari
     */
    function dzSubari($Html, $Lang = "default") {
        if (!$Lang) $Lang = "default";
        $this->__Items = new soSet();

        /* Check that Subari was defined. If not then exit because here is
           nothing to do. */
        $filename = basename(soExtractVal(basename($Html), ".", 1));
        $form_path = dzGetIniPath(DZID_FORM_SOURCE_FOLDER);
        $ini_stream = "file://".$form_path."/".$filename.".".DZ_SUBARI_EXT;
        $ini = new soIni($ini_stream);
        if ($ini->Error) return;  // <-- EXIT !!!!
        $sub_props = array(DZS_EXE, DZS_DISPOSE, DZS_MODES,
                           DZS_USER_LEVEL, DZS_USER_GROUPS);

        /* Loop all sections, subari titles */
        $scount = $ini->getSectionCount();
        for ($s = 0; $s < $scount; $s++) {
            $item = new soSet();
            $section_name = $ini->getSectionName($s);

            /* Loop and get other properties than title for an entry */
            foreach ($sub_props as $prop) {
                $value = $ini->getValue($s, $prop, SOINI_NOSUB);
                if ($value) $item->addItem($prop, $value);
            }; // foreach

            /* Get/set title */
            $title = $ini->getValue($s, DZS_TITLE, $Lang);
            if (!$title) $title = $ini->getValue($s, DZS_TITLE, "default");
            if (!$title) $title = $section_name;
            $item->addItem(DZS_TITLE, $title);

            /* Store the item */
            $this->__Items->addItem($title, $item);
        }; // for $s
    } // constructor


    /** Returns subari titles for specified mode.
     *  @param int $Mode Current mode
     *  @param array/string $UserGroups User's groups
     *  @param int $UserLevel User's level
     *  @return array/string Titles for the subari
     */
    function getTitles($Mode, $UserGroups, $UserLevel) {
        $result = array();

        /* Change mode to string presentation */
        $Mode = dzGetModeName($Mode);
        $arr = $this->__Items->getAsArray();
        $keys = $this->__Items->getKeys();
        foreach ($keys as $key) {
            /* get the modes, groups and level of the subari item */
            $item = $this->__Items->getItem($key);
            $item_modes = soExplode($item->getItem(DZS_MODES), ",");
            $groups = soExplode($item->getItem(DZS_GROUPS), ",");
            $level = $item->getItem(DZS_LEVEL);
            $strict = dzGetIniItem(DZIS_GENERAL,
                                   DZID_STRICT_ACCESS, "app", "bool");

            /* Check if current mode is listed and user has access */
            if (((!count($item_modes)) ||
                (in_array($Mode, $item_modes))) &&
                (dzHasAccess($groups, $level,
                             $UserGroups, $UserLevel, $strict))) {

                /* Add title to the list */
                $title = $item->getItem(DZS_TITLE);
                if (!$title) $title = $key;
                $result[] = $title;
            } // if
        } // foreach

        return $result;
    } // getTitles


    /** Gets subari item
     *  Gets the item from the subari defition. The item is identified with its
     *  title.
     *  @param string $Title Title of the item to get
     *  @return soSet Properties for the Subari item
     */
    function getItem($Title) {
        return $this->__Items->getItem($Title);
    } // getItem


} //dzSubari

?>
